using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System.Web.Http;
using wnet.admin.dbhelper;

using wnet.admin.Models;

using System;
using wnet.admin.wentcommon;
using Models;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using wnet.admin.Models.business;
using System.Linq;

namespace wnet.admin.Controllers.ywBusiness
{
    /// <summary>
    /// 账户管理
    /// </summary>
    [TokenAuth]
    [Route("business/sysuser/{action}")]
    public class sys_userController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public sys_userController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }

        /// <summary>
        /// 账户管理-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpGet]
        public object getselectoptlist()
        {


            var predicate = Expressionable.Create<sys_user>(); 
            predicate = predicate.And(it => it.status == 1);
            var query = _sqlSugarClient.Queryable<sys_user>()
                .Where(predicate.ToExpression())
                .Select((u) => new
                {
                    id = u.id,
                    name = u.name 
                });

            var datalist = query.ToList();

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }




        /// <summary>
        /// 账户管理-列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        public object list(sys_menuQueryDto parm)
        {
            if (parm == null)
            {
                parm = new sys_menuQueryDto();
            }

            var predicate = Expressionable.Create<sys_user>();

            predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.name), it => it.name.Contains(parm.name));
            predicate = predicate.AndIF(parm.status != null, it => it.status == parm.status);
            var query = _sqlSugarClient.Queryable<sys_user>()
                .Where(predicate.ToExpression())
                .Select((u) => new sys_user
                {
                    id = u.id.SelectAll(),

                });

            var datalist = query.ToPage(parm);

            var data = new { code = 200, msg = "成功", data = datalist };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 账户管理-详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Getsys_user(int id)
        {
            var response = _sqlSugarClient.Queryable<sys_user>()
                .Where(q => q.id == id).First();

            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 账户管理-添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Addsys_user([FromBody] sys_user parm)
        {

            parm.createtime = DateTime.Now;
            parm.loginpwd = tools.GetMD5Hash(parm.loginpwd);
            var response = _sqlSugarClient.Insertable<sys_user>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }


        /// <summary>
        /// 账户管理-编辑
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public object Updatesys_user([FromBody] sys_user parm)
        {

            parm.createtime = DateTime.Now;
            if (!string.IsNullOrWhiteSpace(parm.loginpwd))
            {
                parm.loginpwd = tools.GetMD5Hash(parm.loginpwd);

            }
            else
            {
                var userinfo = _sqlSugarClient.Queryable<sys_user>()
              .Where(q => q.id == parm.id).First();
                parm.loginpwd = userinfo.loginpwd;
            }
            var response = _sqlSugarClient.Updateable<sys_user>(parm).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }

        /// <summary>
        /// 账户管理-删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public object Deletesys_user(int id)
        {
            try
            {
                var response = _sqlSugarClient.Queryable<sys_user>()
                     .Where(q => q.id == id).First();
                if (response == null)
                {
                    throw new Exception("数据不存在");
                }
                _sqlSugarClient.Deleteable<sys_user>(response).ExecuteCommand();
                var data = new { code = 200, msg = "成功", data = response };

                return Json(data, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }
            catch (Exception ex)
            {

                var data = new { code = -1, msg = ex.Message, data = "null" };

                return Json(data, new JsonSerializerSettings
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver()
                });
            }
        }
    }
}